﻿using System;
using System.Collections.Generic;
using System.Web;

namespace PhoneStoreWeb.Models
{
    public class ShoppingCartViewModel
    {
        public List<Cart> CartItems { get; set; }
        public decimal CartTotal { get; set; }
    }

    public class ShoppingCartModel
    {
        public string ShoppingCartId { get; private set; }
        private const string CART_SESSION_KEY = "CartId";

        // Create ShoppingCartModel instance
        public static ShoppingCartModel GetCart(HttpContextBase context)
        {
            ShoppingCartModel cart = new ShoppingCartModel();
            // Dictionary key
            // Create unique CartID (either "userName" or "Guid")
            if (context.Session[CART_SESSION_KEY] == null)
            {
                if (!String.IsNullOrEmpty(context.User.Identity.Name))
                {
                    // User is logged in
                    context.Session[CART_SESSION_KEY] = context.User.Identity.Name;
                }
                else
                {
                    // Anonymous user, assign a temporary key value
                    Guid tempCartId = Guid.NewGuid();
                    context.Session[CART_SESSION_KEY] = tempCartId.ToString();
                }
            }
            cart.ShoppingCartId = context.Session[CART_SESSION_KEY].ToString();
            return cart;
        }

        public static void ResetCartSessionKey(HttpContextBase context)
        {
            context.Session[CART_SESSION_KEY] = null;
        }
    }
}